﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.BLL;
using WebApp.BLL.Evaluate;
using WebApp.BLL.Manage;
using WebApp.Common;
using WebApp.Models;
using WebApp.ViewModels;
using WebApp.ViewModels.Evaluate;
using WebApp.Web.Core;
using WebApp.Web.Core.Auth;

namespace kzjd.rycp.WebApp.Areas.Basic.Controllers
{
    public class EvalSettingController : Controller
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger("WebApp.Log");

        #region Index

        [AuthorizeFilter("Evaluate.Setting")]
        public ActionResult Index()
        {
            var service = new T_evaluateService();

            ViewBag.CanEdit = UserAuthBLL.HasAuth("Evaluate.Setting");

            //测评状态
            ViewBag.StatusSelectList = CommonStr.EnumToSelectList(typeof(enmEvalStatus));

            var model = new EvalSettingModel();

            ////当前年度
            //model.CurrentYear = DateTime.Now.Year;

            ////上一年度测评
            //model.PrevEval = service.SelectByYear(DateTime.Now.Year - 1);

            ////测评主记录
            //if (!string.IsNullOrEmpty(id))
            //{
            //    model.Eval = service.SelectByYear(Utils.NvInt(id));
            //    if (model.Eval == null)
            //    {
            //        throw new HttpException(404, "测评不存在！");
            //    }
            //}
            //else
            //{
            //    if (model.PrevEval == null)
            //    {
            //        model.Eval = service.SelectByYear(DateTime.Now.Year);

            //        if (model.Eval == null)
            //        {
            //            model.Eval = new T_evaluate();
            //        }
            //    }
            //    else
            //    {
            //        model.Eval = new T_evaluateService().GetCurrentEval();
            //    }
            //}

            //测评主记录
            model.Eval = new T_evaluateService().GetCurrentEval();

            //车间列表
            if (model.Eval.Id > 0)
            {
                var deptList = new S_departmentService().ListByDeptType(enmDeptType.Worker.ToString());
                model.EvalDeptList = new T_evaluate_deptService().InitDept(model.Eval.Id, deptList);
            }
            else
            {
                model.EvalDeptList = new List<T_evaluate_dept>();
            }

            return View(model);
        }

        #endregion

        #region Save

        [IgnoreAuthorizeFilter]
        [ValidateInput(false)]
        public JsonResult Save(string json)
        {
            try
            {
                var service = new T_evaluateService();
                var deptservice = new T_evaluate_deptService();

                var model = Newtonsoft.Json.JsonConvert.DeserializeObject<EvalSettingModel>(json);

                var eval = service.Find(model.EvalSave.Id);
                model.EvalSave.SetModelData<T_evaluate>(eval);

                var evalDeptList = new List<T_evaluate_dept>();
                foreach (var item in model.EvalDeptList)
                {
                    var itemdata = deptservice.Find(item.Id);
                    itemdata.Password = item.Password;
                    itemdata.Requireusers = item.Requireusers;
                    evalDeptList.Add(itemdata);
                }

                service.SaveSetting(eval, evalDeptList);

                return Json(new AjaxResult(AjaxResult.RESULT_OK, "保存成功！"));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return Json(new AjaxResult(AjaxResult.RESULT_ERROR, ex.Message));
            }
        }

        #endregion

    }
}